######################################################
#Partisanship Interaction- 2014 General Election PA.
# When Women Run, Voters Will Follow (Sometimes): 
##Examining the Mobilizing Effect of Female Candidates in the 2014 and 2018 Midterm Elections 
#By Safarpour, Wyckoff Gaynor, Rouse, and Swers #
######################################################

#clean the environment.
rm(list=ls()) 

#Packages.
library(mvtnorm) 
library(stargazer)
library(stats)
library(arm)
library(plyr)
library(Hmisc)
library(dplyr)
library(margins)

#setwd
setwd("/Users/ACS/Dropbox/When Women Run/Revision_PoliticalBehavior/R&R Part 2/Publication Docs/Replication Data and Code/")

#Step 1. Bring in dataset.
PA<- read.csv("PA2014GeneralDataFinal.csv", header=TRUE)

#Step 2. Check length.
length(PA$V1)

#Step 3. Run Model.
m1<-glm(voted2014general~ FemaleDemocrat+FemaleRepublican+
          female2+FemaleDemocrat*female2+FemaleRepublican*female2+
          Millennials2014+FemaleDemocrat*Millennials2014+FemaleRepublican*Millennials2014+
          GenerationX2014+FemaleDemocrat*GenerationX2014+FemaleRepublican*GenerationX2014+
          SilentGeneration2014+FemaleDemocrat*SilentGeneration2014+FemaleRepublican*SilentGeneration2014+
          rep + FemaleDemocrat*rep+FemaleRepublican*rep+
          otherparty+ FemaleDemocrat*otherparty+FemaleRepublican*otherparty+
          female2*rep+ female2*otherparty+
          voted2012+obamavote2012+
          otherparty*FemaleDemocrat*female2+
          otherparty*FemaleRepublican*female2+
          rep*FemaleDemocrat*female2+
          rep*FemaleRepublican*female2+
          totalreceipts, 
        data=PA, family=binomial(link="logit"))
summary(m1)

summary(margins(m1, variables=c("FemaleDemocrat"))) 
summary(margins(m1, variables=c("FemaleRepublican"))) 
summary(margins(m1, variables=c("FemaleDemocrat"), at=list(female2=0:1, rep=0, otherparty=0))) 

stargazer(m1, out="PA2014GeneralPartyInteractionRevised.html",
          type="html",
          covariate.labels = c("Female Democratic Candidate", 
                               "Female Republican Candidate",
                               "Female", 
                               "Millennials", "Generation X","Silent Generation", 
                               "Republican", "Other Party",
                               "Voted in 2012 General Election", "County Obama Vote 2012", 
                               "Total Money Raised",
                               "Female Democratic Candidate*Female",
                               "Female Republican Candidate*Female",
                               "Female Democratic Candidate*Millennials",
                               "Female Republican Candidate*Millennials",
                               "Female Democratic Candidate*Generation X",                               
                               "Female Republican Candidate*Generation X",
                               "Female Democratic Candidate*Silent Generation",
                               "Female Republican Candidate*Silent Generation",
                               "Female Democratic Candidate*Republican",
                               "Female Republican Candidate*Republican",
                               "Female Democratic Candidate*Other Party",
                               "Female Republican Candidate*Other Party",
                               "Female*Republican", "Female*Other Party",
                               "Other Party*Female Democratic Candidate*Female",
                               "Other Party*Female Republican Candidate*Female",
                               "Republican*Female Democratic Candidate*Female",
                               "Republican*Female Republican Candidate*Female"),
          dep.var.labels="Voted in PA 2014 General Election", column.labels = "",
          title = "Effects of candidate gender, generation, gender,  party, prior voting, county Obama vote share, and total money raised on 2014 Midterm Turnout in PA",
          notes        = "Results from logistic regression. Standard errors in parentheses. Baseline age category: Baby Boomers. Baseline party: Democrats. Baseline district: only male House candidates. Baseline gender: Male. The only district with a female Republican candidate was district 1.", 
          notes.append = TRUE, notes.align = "l", digits=3, single.row = T)

sample <- PA[complete.cases(PA$voted2014general, PA$FemaleDemocrat, PA$FemaleRepublican, PA$SilentGeneration2014, 
                            PA$Millennials2014, PA$GenerationX2014, PA$female2, PA$rep, PA$otherparty,
                            PA$voted2012, PA$obamavote2012,
                            PA$totalreceipts)==T,] # Regression sample
str(sample)

##Overall effect for m1.
preds <- predict(m1, type = "response")
options(digits=4)
summary(preds) 

n_draws <- 1000
set.seed(1714)
vcovm1<- vcov(m1)
coefm1<-coef(m1)
sim_coefs_m1 <- rmvnorm(n_draws, coefm1, vcovm1) #Specify the 1,000 simulated coefficients.
rbind(coef(m1), apply(sim_coefs_m1, 2, mean)) # Check they are close to original

ppwomen0m1s <- NULL
for (i in 1:n_draws) {
  ppwomen0m1s[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*0 + #FemaleRepublican
                                    sim_coefs_m1[i,4]*sample$female2 + 
                                    sim_coefs_m1[i,5]*sample$Millennials2014 +
                                    sim_coefs_m1[i,6]*sample$GenerationX2014 +
                                    sim_coefs_m1[i,7]*sample$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*sample$rep +
                                    sim_coefs_m1[i,9]*sample$otherparty+
                                    sim_coefs_m1[i,10]*sample$voted2012+
                                    sim_coefs_m1[i,11]*sample$obamavote2012+
                                    sim_coefs_m1[i,12]*sample$totalreceipts+
                                    sim_coefs_m1[i,13]*sample$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*sample$female2*0+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*sample$Millennials2014*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*sample$Millennials2014*0 + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*sample$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*sample$GenerationX2014*0 + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*sample$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*sample$SilentGeneration2014*0+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*sample$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*sample$rep*0 + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*sample$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*sample$otherparty*0+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*sample$female2*sample$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*sample$female2*sample$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*sample$female2*sample$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*sample$female2*sample$otherparty*0+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*sample$female2*sample$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*sample$female2*sample$rep*0 #FemaleRepublican:female2:rep
  ))
}
mean(ppwomen0m1s) # 

ppwomen1m1.s <- NULL
for (i in 1:n_draws) {
  ppwomen1m1.s[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                     sim_coefs_m1[i,3]*1 + #FemaleRepublican
                                     sim_coefs_m1[i,4]*sample$female2 + 
                                     sim_coefs_m1[i,5]*sample$Millennials2014 +
                                     sim_coefs_m1[i,6]*sample$GenerationX2014 +
                                     sim_coefs_m1[i,7]*sample$SilentGeneration2014+
                                     sim_coefs_m1[i,8]*sample$rep +
                                     sim_coefs_m1[i,9]*sample$otherparty+
                                     sim_coefs_m1[i,10]*sample$voted2012+
                                     sim_coefs_m1[i,11]*sample$obamavote2012+
                                     sim_coefs_m1[i,12]*sample$totalreceipts+
                                     sim_coefs_m1[i,13]*sample$female2*1+ #Female Democrat*Female
                                     sim_coefs_m1[i,14]*sample$female2*1+ #FemaleRepublican:female2
                                     sim_coefs_m1[i,15]*sample$Millennials2014*1 + #FemaleDemocrat:Millennials
                                     sim_coefs_m1[i,16]*sample$Millennials2014*1 + #FemaleRepublican:Millennials
                                     sim_coefs_m1[i,17]*sample$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                                     sim_coefs_m1[i,18]*sample$GenerationX2014*1 + #FemaleRepublican:GenerationX
                                     sim_coefs_m1[i,19]*sample$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                                     sim_coefs_m1[i,20]*sample$SilentGeneration2014*1+ #FemaleRepublican:SilentGeneration
                                     sim_coefs_m1[i,21]*sample$rep*1 + #FemaleDemocrat:rep
                                     sim_coefs_m1[i,22]*sample$rep*1 + #FemaleRepublican:rep
                                     sim_coefs_m1[i,23]*sample$otherparty*1+ #FemaleDemocrat:otherparty
                                     sim_coefs_m1[i,24]*sample$otherparty*1+ #FemaleRepublican:otherparty
                                     sim_coefs_m1[i,25]*sample$female2*sample$rep+ #female2:rep
                                     sim_coefs_m1[i,26]*sample$female2*sample$otherparty+ #female2:otherparty
                                     sim_coefs_m1[i,27]*sample$female2*sample$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                     sim_coefs_m1[i,28]*sample$female2*sample$otherparty*1+ #FemaleRepublican:female2:otherparty
                                     sim_coefs_m1[i,29]*sample$female2*sample$rep*1+ #FemaleDemocrat:female2:rep
                                     sim_coefs_m1[i,30]*sample$female2*sample$rep*1 #FemaleRepublican:female2:rep
  ))
}
mean(ppwomen1m1.s) #


effect.01m1 <-  ppwomen1m1.s-ppwomen0m1s
summary(effect.01m1) #  (0-1 women candidate effect)

# CIs and put results together in matrix
elementsm1 <- list(ppwomen0m1s, ppwomen1m1.s, effect.01m1) # I split these two lines so the code is clear.
lapply(elementsm1, summary) # See summary for each element of the list
cim1 <- do.call("rbind", (lapply(elementsm1, quantile, c(.025,.975)))) # Get quntiles for each

resultsm1 <- cbind(cim1[,1], lapply(elementsm1, mean), cim1[,2])
colnames(resultsm1) <- c("2.5", "Mean", "97.5")   
rownames(resultsm1) <- c("No Women Ran", "Female Candidate", "Effect No Women-Female Candidate")
resultsm1 #

#Drop what we no longer need.
rm(ppwomen0m1s, ppwomen1m1.s, effect.01m1, cim1)

# Store mean effect and 95% CI to visualize results of m1 later.
meaneffect_m1<-resultsm1[[6]]
upperci_m1<-resultsm1[[3]]
lowerci_m1<-resultsm1[[9]]

#Only effect of Female Democrats-------
w0.FDEM<-NULL
w1.FDEM<-NULL

w0.FDEM <- NULL
for (i in 1:n_draws) {
  w0.FDEM[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                sim_coefs_m1[i,3]*sample$FemaleRepublican + #FemaleRepublican (obs val)
                                sim_coefs_m1[i,4]*sample$female2 + 
                                sim_coefs_m1[i,5]*sample$Millennials2014 +
                                sim_coefs_m1[i,6]*sample$GenerationX2014 +
                                sim_coefs_m1[i,7]*sample$SilentGeneration2014+
                                sim_coefs_m1[i,8]*sample$rep +
                                sim_coefs_m1[i,9]*sample$otherparty+
                                sim_coefs_m1[i,10]*sample$voted2012+
                                sim_coefs_m1[i,11]*sample$obamavote2012+
                                sim_coefs_m1[i,12]*sample$totalreceipts+
                                sim_coefs_m1[i,13]*sample$female2*0+ #Female Democrat*Female
                                sim_coefs_m1[i,14]*sample$female2*sample$FemaleRepublican+ #FemaleRepublican:female2
                                sim_coefs_m1[i,15]*sample$Millennials2014*0 + #FemaleDemocrat:Millennials
                                sim_coefs_m1[i,16]*sample$Millennials2014*sample$FemaleRepublican + #FemaleRepublican:Millennials
                                sim_coefs_m1[i,17]*sample$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                                sim_coefs_m1[i,18]*sample$GenerationX2014*sample$FemaleRepublican + #FemaleRepublican:GenerationX
                                sim_coefs_m1[i,19]*sample$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                                sim_coefs_m1[i,20]*sample$SilentGeneration2014*sample$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                sim_coefs_m1[i,21]*sample$rep*0 + #FemaleDemocrat:rep
                                sim_coefs_m1[i,22]*sample$rep*sample$FemaleRepublican + #FemaleRepublican:rep
                                sim_coefs_m1[i,23]*sample$otherparty*0+ #FemaleDemocrat:otherparty
                                sim_coefs_m1[i,24]*sample$otherparty*sample$FemaleRepublican+ #FemaleRepublican:otherparty
                                sim_coefs_m1[i,25]*sample$female2*sample$rep+ #female2:rep
                                sim_coefs_m1[i,26]*sample$female2*sample$otherparty+ #female2:otherparty
                                sim_coefs_m1[i,27]*sample$female2*sample$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                sim_coefs_m1[i,28]*sample$female2*sample$otherparty*sample$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                sim_coefs_m1[i,29]*sample$female2*sample$rep*0+ #FemaleDemocrat:female2:rep
                                sim_coefs_m1[i,30]*sample$female2*sample$rep*sample$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.FDEM) # 

w1.FDEM <- NULL
for (i in 1:n_draws) {
  w1.FDEM[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                sim_coefs_m1[i,3]*sample$FemaleRepublican + #FemaleRepublican (obs val)
                                sim_coefs_m1[i,4]*sample$female2 + 
                                sim_coefs_m1[i,5]*sample$Millennials2014 +
                                sim_coefs_m1[i,6]*sample$GenerationX2014 +
                                sim_coefs_m1[i,7]*sample$SilentGeneration2014+
                                sim_coefs_m1[i,8]*sample$rep +
                                sim_coefs_m1[i,9]*sample$otherparty+
                                sim_coefs_m1[i,10]*sample$voted2012+
                                sim_coefs_m1[i,11]*sample$obamavote2012+
                                sim_coefs_m1[i,12]*sample$totalreceipts+
                                sim_coefs_m1[i,13]*sample$female2*1+ #Female Democrat*Female
                                sim_coefs_m1[i,14]*sample$female2*sample$FemaleRepublican+ #FemaleRepublican:female2
                                sim_coefs_m1[i,15]*sample$Millennials2014*1 + #FemaleDemocrat:Millennials
                                sim_coefs_m1[i,16]*sample$Millennials2014*sample$FemaleRepublican + #FemaleRepublican:Millennials
                                sim_coefs_m1[i,17]*sample$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                                sim_coefs_m1[i,18]*sample$GenerationX2014*sample$FemaleRepublican + #FemaleRepublican:GenerationX
                                sim_coefs_m1[i,19]*sample$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                                sim_coefs_m1[i,20]*sample$SilentGeneration2014*sample$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                sim_coefs_m1[i,21]*sample$rep*1 + #FemaleDemocrat:rep
                                sim_coefs_m1[i,22]*sample$rep*sample$FemaleRepublican + #FemaleRepublican:rep
                                sim_coefs_m1[i,23]*sample$otherparty*1+ #FemaleDemocrat:otherparty
                                sim_coefs_m1[i,24]*sample$otherparty*sample$FemaleRepublican+ #FemaleRepublican:otherparty
                                sim_coefs_m1[i,25]*sample$female2*sample$rep+ #female2:rep
                                sim_coefs_m1[i,26]*sample$female2*sample$otherparty+ #female2:otherparty
                                sim_coefs_m1[i,27]*sample$female2*sample$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                sim_coefs_m1[i,28]*sample$female2*sample$otherparty*sample$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                sim_coefs_m1[i,29]*sample$female2*sample$rep*1+ #FemaleDemocrat:female2:rep
                                sim_coefs_m1[i,30]*sample$female2*sample$rep*sample$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.FDEM) 

effect.FDEM	<- w1.FDEM - w0.FDEM 
quantile(effect.FDEM, c(.025,.975)) #to get CIs; 
mean(effect.FDEM) #to get mean effect

#Drop what we no longer need.
rm(w1.FDEM, w0.FDEM )

#Only Effect of Female Republicans------
w0.FGOP<-NULL
w1.FGOP<-NULL

for (i in 1:n_draws) {
  w0.FGOP[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*sample$FemaleDemocrat + #Female Democrat (obs val)
                                sim_coefs_m1[i,3]*0 + #FemaleRepublican 
                                sim_coefs_m1[i,4]*sample$female2 + 
                                sim_coefs_m1[i,5]*sample$Millennials2014 +
                                sim_coefs_m1[i,6]*sample$GenerationX2014 +
                                sim_coefs_m1[i,7]*sample$SilentGeneration2014+
                                sim_coefs_m1[i,8]*sample$rep +
                                sim_coefs_m1[i,9]*sample$otherparty+
                                sim_coefs_m1[i,10]*sample$voted2012+
                                sim_coefs_m1[i,11]*sample$obamavote2012+
                                sim_coefs_m1[i,12]*sample$totalreceipts+
                                sim_coefs_m1[i,13]*sample$female2*sample$FemaleDemocrat+ #Female Democrat*Female
                                sim_coefs_m1[i,14]*sample$female2*0+ #FemaleRepublican:female2
                                sim_coefs_m1[i,15]*sample$Millennials2014*sample$FemaleDemocrat + #FemaleDemocrat:Millennials
                                sim_coefs_m1[i,16]*sample$Millennials2014*0 + #FemaleRepublican:Millennials
                                sim_coefs_m1[i,17]*sample$GenerationX2014*sample$FemaleDemocrat + #FemaleDemocrat:GenerationX
                                sim_coefs_m1[i,18]*sample$GenerationX2014*0 + #FemaleRepublican:GenerationX
                                sim_coefs_m1[i,19]*sample$SilentGeneration2014*sample$FemaleDemocrat+ #FemaleDemocrat:SilentGeneration
                                sim_coefs_m1[i,20]*sample$SilentGeneration2014*0+ #FemaleRepublican:SilentGeneration
                                sim_coefs_m1[i,21]*sample$rep*sample$FemaleDemocrat + #FemaleDemocrat:rep
                                sim_coefs_m1[i,22]*sample$rep*0 + #FemaleRepublican:rep
                                sim_coefs_m1[i,23]*sample$otherparty*sample$FemaleDemocrat+ #FemaleDemocrat:otherparty
                                sim_coefs_m1[i,24]*sample$otherparty*0+ #FemaleRepublican:otherparty
                                sim_coefs_m1[i,25]*sample$female2*sample$rep+ #female2:rep
                                sim_coefs_m1[i,26]*sample$female2*sample$otherparty+ #female2:otherparty
                                sim_coefs_m1[i,27]*sample$female2*sample$otherparty*sample$FemaleDemocrat+ #FemaleDemocrat:female2:otherparty
                                sim_coefs_m1[i,28]*sample$female2*sample$otherparty*0+ #FemaleRepublican:female2:otherparty
                                sim_coefs_m1[i,29]*sample$female2*sample$rep*sample$FemaleDemocrat+ #FemaleDemocrat:female2:rep
                                sim_coefs_m1[i,30]*sample$female2*sample$rep*0 #FemaleRepublican:female2:rep
  ))
}
mean(w0.FGOP) # 

w1.FGOP <- NULL
for (i in 1:n_draws) {
  w1.FGOP[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*sample$FemaleDemocrat + #Female Democrat (obs val)
                                sim_coefs_m1[i,3]*1 + #FemaleRepublican 
                                sim_coefs_m1[i,4]*sample$female2 + 
                                sim_coefs_m1[i,5]*sample$Millennials2014 +
                                sim_coefs_m1[i,6]*sample$GenerationX2014 +
                                sim_coefs_m1[i,7]*sample$SilentGeneration2014+
                                sim_coefs_m1[i,8]*sample$rep +
                                sim_coefs_m1[i,9]*sample$otherparty+
                                sim_coefs_m1[i,10]*sample$voted2012+
                                sim_coefs_m1[i,11]*sample$obamavote2012+
                                sim_coefs_m1[i,12]*sample$totalreceipts+
                                sim_coefs_m1[i,13]*sample$female2*sample$FemaleDemocrat+ #Female Democrat*Female
                                sim_coefs_m1[i,14]*sample$female2*1+ #FemaleRepublican:female2
                                sim_coefs_m1[i,15]*sample$Millennials2014*sample$FemaleDemocrat + #FemaleDemocrat:Millennials
                                sim_coefs_m1[i,16]*sample$Millennials2014*1 + #FemaleRepublican:Millennials
                                sim_coefs_m1[i,17]*sample$GenerationX2014*sample$FemaleDemocrat + #FemaleDemocrat:GenerationX
                                sim_coefs_m1[i,18]*sample$GenerationX2014*1 + #FemaleRepublican:GenerationX
                                sim_coefs_m1[i,19]*sample$SilentGeneration2014*sample$FemaleDemocrat+ #FemaleDemocrat:SilentGeneration
                                sim_coefs_m1[i,20]*sample$SilentGeneration2014*1+ #FemaleRepublican:SilentGeneration
                                sim_coefs_m1[i,21]*sample$rep*sample$FemaleDemocrat + #FemaleDemocrat:rep
                                sim_coefs_m1[i,22]*sample$rep*1 + #FemaleRepublican:rep
                                sim_coefs_m1[i,23]*sample$otherparty*sample$FemaleDemocrat+ #FemaleDemocrat:otherparty
                                sim_coefs_m1[i,24]*sample$otherparty*1+ #FemaleRepublican:otherparty
                                sim_coefs_m1[i,25]*sample$female2*sample$rep+ #female2:rep
                                sim_coefs_m1[i,26]*sample$female2*sample$otherparty+ #female2:otherparty
                                sim_coefs_m1[i,27]*sample$female2*sample$otherparty*sample$FemaleDemocrat+ #FemaleDemocrat:female2:otherparty
                                sim_coefs_m1[i,28]*sample$female2*sample$otherparty*1+ #FemaleRepublican:female2:otherparty
                                sim_coefs_m1[i,29]*sample$female2*sample$rep*sample$FemaleDemocrat+ #FemaleDemocrat:female2:rep
                                sim_coefs_m1[i,30]*sample$female2*sample$rep*1 #FemaleRepublican:female2:rep
  ))
}
mean(w1.FGOP) #
effect.FGOP	<- w1.FGOP - w0.FGOP 
quantile(effect.FGOP, c(.025,.975)) #to get CIs; 
mean(effect.FGOP) #to get mean effect

#Drop what we no longer need.
rm(w0.FGOP, w1.FGOP )

#Effect of Female Democrats on Democrats overall----
DEM <- subset(sample, dem==1) 

w0.DEM<-NULL
w1.DEM<- NULL

for (i in 1:n_draws) {
  w0.DEM[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                               sim_coefs_m1[i,3]*DEM$FemaleRepublican + #FemaleRepublican (obs val)
                               sim_coefs_m1[i,4]*DEM$female2 + 
                               sim_coefs_m1[i,5]*DEM$Millennials2014 +
                               sim_coefs_m1[i,6]*DEM$GenerationX2014 +
                               sim_coefs_m1[i,7]*DEM$SilentGeneration2014+
                               sim_coefs_m1[i,8]*DEM$rep +
                               sim_coefs_m1[i,9]*DEM$otherparty+
                               sim_coefs_m1[i,10]*DEM$voted2012+
                               sim_coefs_m1[i,11]*DEM$obamavote2012+
                               sim_coefs_m1[i,12]*DEM$totalreceipts+
                               sim_coefs_m1[i,13]*DEM$female2*0+ #Female Democrat*Female
                               sim_coefs_m1[i,14]*DEM$female2*DEM$FemaleRepublican+ #FemaleRepublican:female2
                               sim_coefs_m1[i,15]*DEM$Millennials2014*0 + #FemaleDemocrat:Millennials
                               sim_coefs_m1[i,16]*DEM$Millennials2014*DEM$FemaleRepublican + #FemaleRepublican:Millennials
                               sim_coefs_m1[i,17]*DEM$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                               sim_coefs_m1[i,18]*DEM$GenerationX2014*DEM$FemaleRepublican + #FemaleRepublican:GenerationX
                               sim_coefs_m1[i,19]*DEM$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                               sim_coefs_m1[i,20]*DEM$SilentGeneration2014*DEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                               sim_coefs_m1[i,21]*DEM$rep*0 + #FemaleDemocrat:rep
                               sim_coefs_m1[i,22]*DEM$rep*DEM$FemaleRepublican + #FemaleRepublican:rep
                               sim_coefs_m1[i,23]*DEM$otherparty*0+ #FemaleDemocrat:otherparty
                               sim_coefs_m1[i,24]*DEM$otherparty*DEM$FemaleRepublican+ #FemaleRepublican:otherparty
                               sim_coefs_m1[i,25]*DEM$female2*DEM$rep+ #female2:rep
                               sim_coefs_m1[i,26]*DEM$female2*DEM$otherparty+ #female2:otherparty
                               sim_coefs_m1[i,27]*DEM$female2*DEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                               sim_coefs_m1[i,28]*DEM$female2*DEM$otherparty*DEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                               sim_coefs_m1[i,29]*DEM$female2*DEM$rep*0+ #FemaleDemocrat:female2:rep
                               sim_coefs_m1[i,30]*DEM$female2*DEM$rep*DEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.DEM) # 


for (i in 1:n_draws) {
  w1.DEM[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                               sim_coefs_m1[i,3]*DEM$FemaleRepublican + #FemaleRepublican (obs val)
                               sim_coefs_m1[i,4]*DEM$female2 + 
                               sim_coefs_m1[i,5]*DEM$Millennials2014 +
                               sim_coefs_m1[i,6]*DEM$GenerationX2014 +
                               sim_coefs_m1[i,7]*DEM$SilentGeneration2014+
                               sim_coefs_m1[i,8]*DEM$rep +
                               sim_coefs_m1[i,9]*DEM$otherparty+
                               sim_coefs_m1[i,10]*DEM$voted2012+
                               sim_coefs_m1[i,11]*DEM$obamavote2012+
                               sim_coefs_m1[i,12]*DEM$totalreceipts+
                               sim_coefs_m1[i,13]*DEM$female2*1+ #Female Democrat*Female
                               sim_coefs_m1[i,14]*DEM$female2*DEM$FemaleRepublican+ #FemaleRepublican:female2
                               sim_coefs_m1[i,15]*DEM$Millennials2014*1 + #FemaleDemocrat:Millennials
                               sim_coefs_m1[i,16]*DEM$Millennials2014*DEM$FemaleRepublican + #FemaleRepublican:Millennials
                               sim_coefs_m1[i,17]*DEM$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                               sim_coefs_m1[i,18]*DEM$GenerationX2014*DEM$FemaleRepublican + #FemaleRepublican:GenerationX
                               sim_coefs_m1[i,19]*DEM$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                               sim_coefs_m1[i,20]*DEM$SilentGeneration2014*DEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                               sim_coefs_m1[i,21]*DEM$rep*1 + #FemaleDemocrat:rep
                               sim_coefs_m1[i,22]*DEM$rep*DEM$FemaleRepublican + #FemaleRepublican:rep
                               sim_coefs_m1[i,23]*DEM$otherparty*1+ #FemaleDemocrat:otherparty
                               sim_coefs_m1[i,24]*DEM$otherparty*DEM$FemaleRepublican+ #FemaleRepublican:otherparty
                               sim_coefs_m1[i,25]*DEM$female2*DEM$rep+ #female2:rep
                               sim_coefs_m1[i,26]*DEM$female2*DEM$otherparty+ #female2:otherparty
                               sim_coefs_m1[i,27]*DEM$female2*DEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                               sim_coefs_m1[i,28]*DEM$female2*DEM$otherparty*DEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                               sim_coefs_m1[i,29]*DEM$female2*DEM$rep*1+ #FemaleDemocrat:female2:rep
                               sim_coefs_m1[i,30]*DEM$female2*DEM$rep*DEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.DEM) #


effect.DEM	<- w1.DEM - w0.DEM 
quantile(effect.DEM, c(.025,.975)) #to get CIs; 
mean(effect.DEM) #to get mean effect

#Drop what we no longer need.
rm(DEM, w1.DEM, w0.DEM  )

#Effect of Female Democrats on Democratic Men------
fDmDEM <- subset(sample, dem==1 & female2==0)
w0.fDmDEMm1	<- NULL
w1.fDmDEMm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDmDEMm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                    sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                    sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*fDmDEM$rep +
                                    sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                    sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                    sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,13]*fDmDEM$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*fDmDEM$Millennials2014*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDmDEMm1) 


for (i in 1:n_draws) {
  w1.fDmDEMm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                    sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                    sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*fDmDEM$rep +
                                    sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                    sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                    sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,13]*fDmDEM$female2*1+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*fDmDEM$Millennials2014*1 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDmDEMm1) 


effect.fDmDEMm1	<- w1.fDmDEMm1 - w0.fDmDEMm1 
quantile(effect.fDmDEMm1, c(.025,.975)) #to get CIs; 
mean(effect.fDmDEMm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, ww1.fDmDEMm1, w0.fDmDEMm1  )

#Effect of Female Democrats on Democratic Women------
fDmDEM <- subset(sample, dem==1 & female2==1)
w0.fDfDEMm1	<- NULL
w1.fDfDEMm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDfDEMm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                    sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                    sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*fDmDEM$rep +
                                    sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                    sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                    sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,13]*fDmDEM$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*fDmDEM$Millennials2014*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDfDEMm1) 


for (i in 1:n_draws) {
  w1.fDfDEMm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                    sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                    sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*fDmDEM$rep +
                                    sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                    sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                    sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,13]*fDmDEM$female2*1+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*fDmDEM$Millennials2014*1 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDfDEMm1) 


effect.fDfDEMm1	<- w1.fDfDEMm1 - w0.fDfDEMm1 
quantile(effect.fDfDEMm1, c(.025,.975)) #to get CIs; 
mean(effect.fDfDEMm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDfDEMm1, w0.fDfDEMm1)

#Effect of Female Democrats on Republican Men-------
fDmDEM <- subset(sample, rep==1 & female2==0)
w0.fDmREPm1	<- NULL
w1.fDmREPm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDmREPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                    sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                    sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*fDmDEM$rep +
                                    sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                    sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                    sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,13]*fDmDEM$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*fDmDEM$Millennials2014*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDmREPm1) 


for (i in 1:n_draws) {
  w1.fDmREPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                    sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                    sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*fDmDEM$rep +
                                    sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                    sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                    sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,13]*fDmDEM$female2*1+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*fDmDEM$Millennials2014*1 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDmREPm1) 


effect.fDmREPm1	<- w1.fDmREPm1 - w0.fDmREPm1 
quantile(effect.fDmREPm1, c(.025,.975)) #to get CIs; 
mean(effect.fDmREPm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDmREPm1, w0.fDmREPm1)

#Effect of Female Democrats on Republican Women------
fDmDEM <- subset(sample, rep==1 & female2==1)
w0.fDfREPm1	<- NULL
w1.fDfREPm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDfREPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                    sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                    sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*fDmDEM$rep +
                                    sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                    sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                    sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,13]*fDmDEM$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*fDmDEM$Millennials2014*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDfREPm1) # 


for (i in 1:n_draws) {
  w1.fDfREPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                    sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                    sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                    sim_coefs_m1[i,8]*fDmDEM$rep +
                                    sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                    sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                    sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,13]*fDmDEM$female2*1+ #Female Democrat*Female
                                    sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,15]*fDmDEM$Millennials2014*1 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,21]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,23]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDfREPm1) 

effect.fDfREPm1	<- w1.fDfREPm1 - w0.fDfREPm1 
quantile(effect.fDfREPm1, c(.025,.975)) #to get CIs; 
mean(effect.fDfREPm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDfREPm1, w0.fDfREPm1)

#Effect of Female Democrats on Other Party Men-------
fDmDEM <- subset(sample, otherparty==1 & female2==0)
w0.fDmOPm1	<- NULL
w1.fDmOPm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDmOPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                   sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                   sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                   sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                   sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                   sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                   sim_coefs_m1[i,8]*fDmDEM$rep +
                                   sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                   sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                   sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                   sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                   sim_coefs_m1[i,13]*fDmDEM$female2*0+ #Female Democrat*Female
                                   sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                   sim_coefs_m1[i,15]*fDmDEM$Millennials2014*0 + #FemaleDemocrat:Millennials
                                   sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                   sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                                   sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                   sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                                   sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                   sim_coefs_m1[i,21]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                   sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                   sim_coefs_m1[i,23]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                   sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                   sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                   sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                   sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                   sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                   sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                   sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDmOPm1) # 


for (i in 1:n_draws) {
  w1.fDmOPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                   sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                   sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                   sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                   sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                   sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                   sim_coefs_m1[i,8]*fDmDEM$rep +
                                   sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                   sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                   sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                   sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                   sim_coefs_m1[i,13]*fDmDEM$female2*1+ #Female Democrat*Female
                                   sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                   sim_coefs_m1[i,15]*fDmDEM$Millennials2014*1 + #FemaleDemocrat:Millennials
                                   sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                   sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                                   sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                   sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                                   sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                   sim_coefs_m1[i,21]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                   sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                   sim_coefs_m1[i,23]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                   sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                   sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                   sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                   sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                   sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                   sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                   sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDmOPm1) 

effect.fDmOPm1	<- w1.fDmOPm1 - w0.fDmOPm1 
quantile(effect.fDmOPm1, c(.025,.975)) #to get CIs; 
mean(effect.fDmOPm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDmOPm1, w0.fDmOPm1)

#Effect of Female Democrats on Other Party Women------
fDmDEM <- subset(sample, otherparty==1 & female2==1)
w0.fDfOPm1	<- NULL
w1.fDfOPm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDfOPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                   sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                   sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                   sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                   sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                   sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                   sim_coefs_m1[i,8]*fDmDEM$rep +
                                   sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                   sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                   sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                   sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                   sim_coefs_m1[i,13]*fDmDEM$female2*0+ #Female Democrat*Female
                                   sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                   sim_coefs_m1[i,15]*fDmDEM$Millennials2014*0 + #FemaleDemocrat:Millennials
                                   sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                   sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*0 + #FemaleDemocrat:GenerationX
                                   sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                   sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*0+ #FemaleDemocrat:SilentGeneration
                                   sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                   sim_coefs_m1[i,21]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                   sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                   sim_coefs_m1[i,23]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                   sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                   sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                   sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                   sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                   sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                   sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                   sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDfOPm1) 


for (i in 1:n_draws) {
  w1.fDfOPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                   sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                   sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                   sim_coefs_m1[i,5]*fDmDEM$Millennials2014 +
                                   sim_coefs_m1[i,6]*fDmDEM$GenerationX2014 +
                                   sim_coefs_m1[i,7]*fDmDEM$SilentGeneration2014+
                                   sim_coefs_m1[i,8]*fDmDEM$rep +
                                   sim_coefs_m1[i,9]*fDmDEM$otherparty+
                                   sim_coefs_m1[i,10]*fDmDEM$voted2012+
                                   sim_coefs_m1[i,11]*fDmDEM$obamavote2012+
                                   sim_coefs_m1[i,12]*fDmDEM$totalreceipts+
                                   sim_coefs_m1[i,13]*fDmDEM$female2*1+ #Female Democrat*Female
                                   sim_coefs_m1[i,14]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                   sim_coefs_m1[i,15]*fDmDEM$Millennials2014*1 + #FemaleDemocrat:Millennials
                                   sim_coefs_m1[i,16]*fDmDEM$Millennials2014*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                   sim_coefs_m1[i,17]*fDmDEM$GenerationX2014*1 + #FemaleDemocrat:GenerationX
                                   sim_coefs_m1[i,18]*fDmDEM$GenerationX2014*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                   sim_coefs_m1[i,19]*fDmDEM$SilentGeneration2014*1+ #FemaleDemocrat:SilentGeneration
                                   sim_coefs_m1[i,20]*fDmDEM$SilentGeneration2014*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                   sim_coefs_m1[i,21]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                   sim_coefs_m1[i,22]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                   sim_coefs_m1[i,23]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                   sim_coefs_m1[i,24]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                   sim_coefs_m1[i,25]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                   sim_coefs_m1[i,26]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                   sim_coefs_m1[i,27]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                   sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                   sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                   sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDfOPm1) #

effect.fDfOPm1	<- w1.fDfOPm1 - w0.fDfOPm1 
quantile(effect.fDfOPm1, c(.025,.975)) #to get CIs; 
mean(effect.fDfOPm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDfOPm1, w0.fDfOPm1 )

#Effect of Female Republicans on Republican Women-----
fRfR <- subset(sample, rep==1 & female2==1)
w0.fRfRm1	<- NULL
w1.fRfRm1 	<- NULL


for (i in 1:n_draws) {
  w0.fRfRm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*fRfR$FemaleDemocrat + #Female Democrat (obs val)
                                  sim_coefs_m1[i,3]*0 + #FemaleRepublican 
                                  sim_coefs_m1[i,4]*fRfR$female2 + 
                                  sim_coefs_m1[i,5]*fRfR$Millennials2014 +
                                  sim_coefs_m1[i,6]*fRfR$GenerationX2014 +
                                  sim_coefs_m1[i,7]*fRfR$SilentGeneration2014+
                                  sim_coefs_m1[i,8]*fRfR$rep +
                                  sim_coefs_m1[i,9]*fRfR$otherparty+
                                  sim_coefs_m1[i,10]*fRfR$voted2012+
                                  sim_coefs_m1[i,11]*fRfR$obamavote2012+
                                  sim_coefs_m1[i,12]*fRfR$totalreceipts+
                                  sim_coefs_m1[i,13]*fRfR$female2*fRfR$FemaleDemocrat+ #Female Democrat*Female
                                  sim_coefs_m1[i,14]*fRfR$female2*0+ #FemaleRepublican:female2
                                  sim_coefs_m1[i,15]*fRfR$Millennials2014*fRfR$FemaleDemocrat + #FemaleDemocrat:Millennials
                                  sim_coefs_m1[i,16]*fRfR$Millennials2014*0 + #FemaleRepublican:Millennials
                                  sim_coefs_m1[i,17]*fRfR$GenerationX2014*fRfR$FemaleDemocrat + #FemaleDemocrat:GenerationX
                                  sim_coefs_m1[i,18]*fRfR$GenerationX2014*0 + #FemaleRepublican:GenerationX
                                  sim_coefs_m1[i,19]*fRfR$SilentGeneration2014*fRfR$FemaleDemocrat+ #FemaleDemocrat:SilentGeneration
                                  sim_coefs_m1[i,20]*fRfR$SilentGeneration2014*0+ #FemaleRepublican:SilentGeneration
                                  sim_coefs_m1[i,21]*fRfR$rep*fRfR$FemaleDemocrat + #FemaleDemocrat:rep
                                  sim_coefs_m1[i,22]*fRfR$rep*0 + #FemaleRepublican:rep
                                  sim_coefs_m1[i,23]*fRfR$otherparty*fRfR$FemaleDemocrat+ #FemaleDemocrat:otherparty
                                  sim_coefs_m1[i,24]*fRfR$otherparty*0+ #FemaleRepublican:otherparty
                                  sim_coefs_m1[i,25]*fRfR$female2*fRfR$rep+ #female2:rep
                                  sim_coefs_m1[i,26]*fRfR$female2*fRfR$otherparty+ #female2:otherparty
                                  sim_coefs_m1[i,27]*fRfR$female2*fRfR$otherparty*fRfR$FemaleDemocrat+ #FemaleDemocrat:female2:otherparty
                                  sim_coefs_m1[i,28]*fRfR$female2*fRfR$otherparty*0+ #FemaleRepublican:female2:otherparty
                                  sim_coefs_m1[i,29]*fRfR$female2*fRfR$rep*fRfR$FemaleDemocrat+ #FemaleDemocrat:female2:rep
                                  sim_coefs_m1[i,30]*fRfR$female2*fRfR$rep*0 #FemaleRepublican:female2:rep
  ))
}
mean(w0.fRfRm1) 


for (i in 1:n_draws) {
  w1.fRfRm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*fRfR$FemaleDemocrat + #Female Democrat (obs val)
                                  sim_coefs_m1[i,3]*1 + #FemaleRepublican 
                                  sim_coefs_m1[i,4]*fRfR$female2 + 
                                  sim_coefs_m1[i,5]*fRfR$Millennials2014 +
                                  sim_coefs_m1[i,6]*fRfR$GenerationX2014 +
                                  sim_coefs_m1[i,7]*fRfR$SilentGeneration2014+
                                  sim_coefs_m1[i,8]*fRfR$rep +
                                  sim_coefs_m1[i,9]*fRfR$otherparty+
                                  sim_coefs_m1[i,10]*fRfR$voted2012+
                                  sim_coefs_m1[i,11]*fRfR$obamavote2012+
                                  sim_coefs_m1[i,12]*fRfR$totalreceipts+
                                  sim_coefs_m1[i,13]*fRfR$female2*fRfR$FemaleDemocrat+ #Female Democrat*Female
                                  sim_coefs_m1[i,14]*fRfR$female2*1+ #FemaleRepublican:female2
                                  sim_coefs_m1[i,15]*fRfR$Millennials2014*fRfR$FemaleDemocrat + #FemaleDemocrat:Millennials
                                  sim_coefs_m1[i,16]*fRfR$Millennials2014*1 + #FemaleRepublican:Millennials
                                  sim_coefs_m1[i,17]*fRfR$GenerationX2014*fRfR$FemaleDemocrat + #FemaleDemocrat:GenerationX
                                  sim_coefs_m1[i,18]*fRfR$GenerationX2014*1 + #FemaleRepublican:GenerationX
                                  sim_coefs_m1[i,19]*fRfR$SilentGeneration2014*fRfR$FemaleDemocrat+ #FemaleDemocrat:SilentGeneration
                                  sim_coefs_m1[i,20]*fRfR$SilentGeneration2014*1+ #FemaleRepublican:SilentGeneration
                                  sim_coefs_m1[i,21]*fRfR$rep*fRfR$FemaleDemocrat + #FemaleDemocrat:rep
                                  sim_coefs_m1[i,22]*fRfR$rep*1 + #FemaleRepublican:rep
                                  sim_coefs_m1[i,23]*fRfR$otherparty*fRfR$FemaleDemocrat+ #FemaleDemocrat:otherparty
                                  sim_coefs_m1[i,24]*fRfR$otherparty*1+ #FemaleRepublican:otherparty
                                  sim_coefs_m1[i,25]*fRfR$female2*fRfR$rep+ #female2:rep
                                  sim_coefs_m1[i,26]*fRfR$female2*fRfR$otherparty+ #female2:otherparty
                                  sim_coefs_m1[i,27]*fRfR$female2*fRfR$otherparty*fRfR$FemaleDemocrat+ #FemaleDemocrat:female2:otherparty
                                  sim_coefs_m1[i,28]*fRfR$female2*fRfR$otherparty*1+ #FemaleRepublican:female2:otherparty
                                  sim_coefs_m1[i,29]*fRfR$female2*fRfR$rep*fRfR$FemaleDemocrat+ #FemaleDemocrat:female2:rep
                                  sim_coefs_m1[i,30]*fRfR$female2*fRfR$rep*1 #FemaleRepublican:female2:rep
  ))
}
mean(w1.fRfRm1) 

effect.fRfRm1	<- w1.fRfRm1 - w0.fRfRm1 
quantile(effect.fRfRm1, c(.025,.975)) #to get CIs; 
mean(effect.fRfRm1) #to get mean effect

#Drop what we no longer need.
rm(fRfR, w1.fRfRm1, w0.fRfRm1 )

#Step 5. Store the Effects.----------------------
Election<-c(rep("2014 General",11))
Effect<-c(meaneffect_m1, mean(effect.FDEM), mean(effect.FGOP), mean(effect.DEM), mean(effect.fDmDEMm1), mean(effect.fDfDEMm1), mean(effect.fDmREPm1),mean(effect.fDfREPm1),mean(effect.fDmOPm1),mean(effect.fDfOPm1),mean(effect.fRfRm1))
LowerCI<-c(lowerci_m1,  quantile(effect.FDEM, c(.025)), quantile(effect.FGOP, c(.025)), quantile(effect.DEM, c(.025)),quantile(effect.fDmDEMm1, c(.025)),quantile(effect.fDfDEMm1, c(.025)),quantile(effect.fDmREPm1, c(.025)),quantile(effect.fDfREPm1, c(.025)),quantile(effect.fDmOPm1, c(.025)),quantile(effect.fDfOPm1, c(.025)),quantile(effect.fRfRm1, c(.025)))
UpperCI<-c(upperci_m1, quantile(effect.FDEM, c(.975)), quantile(effect.FGOP, c(.975)), quantile(effect.DEM, c(.975)),quantile(effect.fDmDEMm1, c(.975)),quantile(effect.fDfDEMm1, c(.975)),quantile(effect.fDmREPm1, c(.975)),quantile(effect.fDfREPm1, c(.975)),quantile(effect.fDmOPm1, c(.975)),quantile(effect.fDfOPm1, c(.975)),quantile(effect.fRfRm1, c(.975)))
PAgeneral<-data.frame(Effect, UpperCI, LowerCI, Election) 
PAgeneral$`Registrant Subgroup`<-factor(c(1,2,3,4,5,6,7,8,9,10,11), levels=c(1,2,3,4,5,6,7,8,9,10,11),
                                        labels=c("All Female\n Candidates", "Female Democratic\n Candidates", "Female Republican\n Candidates", "Female Democrats\n on all Democrats","Female Democrats\n on Democratic Men", "Female Democrats\n on Democratic Women","Female Democrats\n on Republican Men", "Female Democrats\n on Republican Women", "Female Democrats\n on Other Party Men", "Female Democrats\n on Other Party Women", "Female Republicans\n on Republican Women"), ordered = T)
#write effects to data frame.
write.csv(PAgeneral, "PA2014GeneralEffectsInteractivePartyRevised.csv", row.names=F)
